﻿@using CubicFWNet.Data.ViewModal
@model CubicFWNet.Data.Models.PurchaseCompose
@*@model IEnumerable<ProductTechnologyView>*@
@{
    //ViewBag.Title = "价格核算";
    var pro = ViewBag.Product as CubicFWNet.Data.Models.Product;
    var customer = ViewBag.Customer as CubicFWNet.Data.Models.Customer;
    var pvList = ViewBag.PvList as List<ProductTechnologyView>;
    var customerList = ViewBag.CustomerList as IEnumerable<CubicFWNet.Data.Models.Customer>;
    var operationTime = ViewBag.OperationTime;
    var examine = ViewBag.Examine as CubicFWNet.Data.Models.Examine;
}
<section class="content-header">
    <h1>
        采购单
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index", "Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("PurQuery", "Purchase")">采购查询</a></li>
    </ol>
</section>
<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                @if (Model != null)
                {
                    <form class="form-horizontal" id="frmedit" action="@Url.Action("CompletePurchase", "Purchase")" method="post">
                        <div class="box-body">
                            <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                            <input type="hidden" id="Status" name="Status" value="@Model.Status" />
                            <input type="hidden" id="SupplierId" name="SupplierId" value="@Model.SupplierId" />
                            <input type="hidden" id="ApplyTime" name="ApplyTime" value="@Model.ApplyTime" />
                            <input type="hidden" id="ProductId" name="ProductId" value="@Model.ProductId" />
                            <input type="hidden" id="Type" name="Type" value="@Model.Type" />
                            <input type="hidden" id="ApplyId" name="ApplyId" value="@Model.ApplyId" />@*申请人id*@
                            <div id="proTableListBox">
                                @if (pvList != null)
                                {
                                    foreach (var item in pvList)
                                    {
                                        <div class="col-md-12" style="border-bottom: 1px solid #ccc; margin-top: 8px;">
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">订单号</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <a href="javascript:void(0)" onclick="ShowModal('@item.OrderId')">@item.OrderNum</a>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">工艺</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <a href="javascript:void(0)" class="technologyinfo" data-id="@item.TechnologyId">查看工艺</a>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">数量</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <span>@item.Amount</span>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group col-md-12" style="height: 9px;"></div>
                                    }
                                    <div class="form-group col-md-12" style="height: 1px;"></div>
                                }
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">产品名称</label>
                                <div class="col-sm-9">
                                    @if (Model.Type == 1 && pro != null)
                                    {
                                        <input type="text" class="form-control" id="ProductName" name="ProductName" value="@pro.Name" readonly="readonly" />
                                    }
                                    else
                                    {
                                        <input type="text" class="form-control" id="PurchaseName" name="PurchaseName" value="@Model.ProductName" readonly="readonly" />
                                    }

                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购单号</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="Number" name="Number" value="@Model.Number" readonly="readonly" />
                                </div>
                            </div>
                            @*<div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">供应商</label>
                                    <div class="col-sm-9">
                                        @if (customer != null)
                                        {
                                            if (Model.Status == 2)
                                            {
                                                <select class="form-control" type="select" id="SupplierIdq" name="SupplierIdq">
                                                    <option value="0" selected="selected">==请选择==</option>
                                                    @if (customerList != null)
                                                    {
                                                        foreach (var item in customerList)
                                                        {
                                                            <option value="@item.Id">@item.Name</option>
                                                        }
                                                    }
                                                </select>
                                            }
                                            else
                                            {
                                                <input type="text" class="form-control" id="SupplierName" name="SupplierName" value="@customer.Name" readonly="readonly" />
                                            }
                                        }
                                        else
                                        {
                                            <input type="text" class="form-control" id="SupplierName" name="SupplierName" value="无供应商" readonly="readonly" />
                                        }
                                    </div>
                                </div>*@

                            @if (Model.Status == 2)
                            {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">供应商</label>
                                <div class="col-sm-9">

                                    <input type="hidden" name="SupplierIdq" id="SupplierIdq" value="@customer.Id" />
                                    <span class="btn btn-primary" onclick="ShowCusList()">选择供应商</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <span id="CustomerName">@customer.Name</span>
                                </div>
                            </div>
                            }
                            else
                            {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">供应商</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" readonly value="@customer.Name" />
                                </div>
                            </div>
                            }

                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">采购数量</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control okeydown" id="Amount" name="Amount" value="@Model.Amount" readonly="readonly" />
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">合格数量</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control okeydown" id="StandardAmount" name="StandardAmount" value="@Model.StandardAmount" readonly="readonly" />
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">采购价(单价)</label>
                            <div class="col-sm-9">
                                @if (Model.Status == 2)
                                {
                                    <input type="text" class="form-control okeydown" id="CostPrice" name="CostPrice" value="@Model.CostPrice" />
                                }
                                else
                                {
                                    <input type="text" class="form-control okeydown" id="CostPrice" name="CostPrice" value="@Model.CostPrice" readonly="readonly" />
                                }
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">发票号</label>
                            <div class="col-sm-9">
                                @if (string.IsNullOrEmpty(Model.FinanceNumber))
                                {
                                    <input type="text" class="form-control okeydown" id="FinanceNumber" name="FinanceNumber" value="" readonly="readonly" />
                                }
                                else
                                {
                                    <input type="text" class="form-control okeydown" id="FinanceNumber" name="FinanceNumber" value="@Model.FinanceNumber" readonly="readonly" />
                                }
                            </div>
                        </div>

                        <div class="form-group col-md-6" style="display: none">
                            <label class="col-sm-3 control-label">实际价格</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control okeydown" id="ActualPrice" name="ActualPrice" value="@Model.CostPrice" readonly="readonly" />
                            </div>
                        </div>
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        @*@if (Model.Type == 1 && pro != null)*@
                        @if (pro != null)
                        {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">宽度-mm</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="SizeWidth" name="SizeWidth" value="@pro.SizeWidth" readonly="readonly">
                                </div>
                            </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">长度-mm</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="SizeLenth" name="SizeLenth" value="@pro.SizeLenth" readonly="readonly" />
                                    </div>
                                </div>
                                    <div class="form-group col-md-6">
                                        <label class="col-sm-3 control-label">基本单位</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" id="Unit" name="Unit" value="@pro.Unit" readonly="readonly">
                                        </div>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label class="col-sm-3 control-label">包装单位</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" id="Pack" name="Pack" value="@pro.Pack" readonly="readonly">
                                        </div>
                                    </div>
                                    <div class="form-group col-md-12" style="height: 1px;"></div>
                        }
                        <input type="hidden" id="modalData" value="@Model.Data" readonly="readonly">
                        <div id="json-content">
                        </div>
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">备注</label>
                            <div class="col-sm-9">
                                <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo" readonly="readonly">@Model.Memo</textarea>
                            </div>
                        </div>
                        @if (examine != null)
                        {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">质检备注</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" placeholder="" name="ExamineMemo" id="ExamineMemo" readonly="readonly">@examine.Memo</textarea>
                                </div>
                            </div>
                        }
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        <div class="form-group col-md-6">
                            <label for="operationName" class="col-sm-3 control-label">操作人</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="operationName" name="operationName" value="@Model.CreatorName" readonly="readonly">
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="operationTime" class="col-sm-3 control-label">日期</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="operationTime" name="operationTime" value="@operationTime" readonly="readonly">
                            </div>
                        </div>
                </div>
                        <div class="box-footer" style="width: 80%; margin-left: 20px;">
                            @if (Model.Status == 3)
                            {
                                <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmit(1)">采购完毕</a>
                            }
                            @if (Model.Status == 2)
                            {
                                <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmitInfo()">修改</a>
                            }
                            @*<a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" id="updateprice">修改单价</a>*@
                            @*@if (Model.Status == 10 && string.IsNullOrEmpty(Model.FinanceNumber) && Model.ParentId == null)
                                {
                                    <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmit(2)">填写完毕</a>
                                }*@
                            <span id="historyBack" class="btn btn-primary submitbtn">返回</span>
                            @*<a role="button" href="@Url.Action("PurQuery", "Purchase")" class="btn btn-primary submitbtn">返回</a>*@
                            <div class="text-red" style="margin-top:20px;font-weight:bold">下一步：检验管理-->检验查询</div>
                         </div>
                    </form>
                }
            </div>
        </div>
        </div>
    </section>

    <!--选择供应商-->
    <div class="modal" id="cusList" style="display: none">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">选择供应商</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="form-group col-sm-8">
                            <label for="GetCus" class="col-sm-3 ">搜索</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="GetCus" name="GetCus" placeholder="请输入供应商名称..." onkeyup="GetCus()" />
                            </div>
                        </div>
                    </div>
                    <div id="CusList">
                        @foreach (var item in customerList)
                        {
                            if (item.Id == Model.SupplierId)
                            {
                                <span class="btn btn-primary customerItem" data-cusID="@item.Id" data-cusName="@item.Name" data-sel="1" style="margin:8px" data-dismiss="modal">@item.Name</span>
                            }
                            else
                            {
                                <span class="btn btn-default customerItem" data-cusID="@item.Id" data-cusName="@item.Name" data-sel="0" style="margin:8px" data-dismiss="modal">@item.Name</span>
                            }
                        }
                    </div>
                </div>
                <div class="modal-footer">
                    <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                    <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
                </div>
            </div>
        </div>
    </div>


    <div id="loadingPage" style="display: none;">
        <div class="loading-shade"></div>
        <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
    </div>

    <style>
        .has-error {
            border-color: #dd4b39;
            box-shadow: none;
        }
    </style>


    @section Scripts{
        <script type="text/javascript">
            var subType = 0;
            $(document).ready(function () {
                $("#frmedit").validate({
                    submitHandler: function (form) {
                        var postData = $(form).formSerialize();
                        postData.Data = getParam();
                        $.submitForm({
                            url: $(form).attr("action"),
                            param: postData,
                            success: function () {
                                var status = document.getElementById("Status").value;
                                $.modalAlert("操作成功", "success");
                                if (status == "10") {
                                    window.location.href = '@Url.Action("PurQueryOk", "Purchase")';
                                } else {
                                    window.location.href = '@Url.Action("PurQuery", "Purchase")';
                                }
                            }
                        });
                    }
                });
                var pstatu = document.getElementById("Status").value;
                if (pstatu == "2") {
                    var supplierId = $("#SupplierId").val();
                    $("#SupplierIdq").val(supplierId);
                }

                var yy = document.getElementById("modalData").value;
                if (yy != "") {
                    var status = document.getElementById("Status").value;
                    var data = JSON.parse(document.getElementById("modalData").value);
                    var $content = $('#json-content');
                    var html = "";
                    data.forEach(function (e, i) {
                        if (typeof e.value == "undefined") {
                            e.value = "";
                        }
                        if (typeof e.name == "undefined") {
                            e.name = "";
                        }
                        if (typeof e.des == "undefined") {
                            e.des = "未知";
                        }
                        if (status == "5") {
                            html += "<div class='form-group col-md-6'>";
                            html += "<label for='j_a' class='col-sm-3 control-label'>" + e.des + "</label>";
                            html += "<div class='col-sm-9'>";
                            html += "<input type='text' class='form-control' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' readonly='readonly'>";
                            html += "</div></div>";
                        } else {
                            html += "<div class='form-group col-md-6'>";
                            html += "<label for='j_a' class='col-sm-3 control-label'>" + e.des + "</label>";
                            html += "<div class='col-sm-9'>";
                            if (e.name == "ArrivalTime") {
                                if (status == "10") {
                                    html += "<input type='text' class='form-control Timedatepicker' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' readonly='readonly'>";
                                } else {
                                    html += "<input type='text' class='form-control Timedatepicker' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' required>";
                                }
                            } else {
                                html += "<input type='text' class='form-control' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' readonly='readonly'>";
                            }

                            html += "</div></div>";
                        }

                    });
                    $content.append(html);
                }


                //日期控件
                $(".Timedatepicker").datepicker({
                    autoclose: true,
                    language: 'zh', //汉化
                    format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                    minView: "month"//选择日期后，不会再跳转去选择时分秒
                });
            });
            
            $("#updateprice").click(function() {
                var pid = $("#Id").val();
                var productId = $("#ProductId").val();
                var costPrice = $("#CostPrice").val();
                $.ajax({
                    url: "/Purchase/UpdatePurasePrice",
                    type: "post",
                    data: {
                        Id: pid,
                        ProductId: productId,
                        CostPrice: costPrice
                    },
                    success: function (data) {
                        if (data && JSON.parse(data)) {
                            var d = JSON.parse(data);
                            if (d.Code == 200) {
                                $.modalAlert("修改成功", "success");
                                setTimeout(function () {
                                    location.reload();
                                }, 1000);
                            } else {
                                $.modalAlert("修改失败", "warning");
                            }
                        } else {
                            $.modalAlert("参数错误", "warning");
                        }
                    },
                    error: function (xhr, err, state) {
                        //alert("e");
                    }
                });
            });

            //修改
            function formsubmitInfo() {
                var pid = $("#Id").val();
                var supplierId = $("#SupplierIdq").val();
                var costPrice = $("#CostPrice").val();
                $.ajax({
                    url: "/Purchase/UpdatePuraseInfo",
                    type: "post",
                    data: {
                        Id: pid,
                        SupplierId: supplierId,
                        CostPrice: costPrice
                    },
                    success: function (data) {
                        if (data && JSON.parse(data)) {
                            var d = JSON.parse(data);
                            if (d.Code == 200) {
                                $.modalAlert("修改成功", "success");
                                window.location.href = '@Url.Action("PurQuery", "Purchase")';
                            } else {
                                $.modalAlert("修改失败", "warning");
                            }
                        } else {
                            $.modalAlert("参数错误", "warning");
                        }
                    },
                    error: function (xhr, err, state) {
                        //alert("e");
                    }
                });

            }

            function formsubmit(t) {
                var value = $("#SupplierId").val();
                //var arrivalAmount = $("#ArrivalAmount");
                //var amount = $("#Amount");
                if (value == "" || value == "0") {
                    $.modalAlert("请选择供应商", "warning");
                    return false;
                }
                //if (arrivalAmount > amount) {
                //    $.modalAlert("到货数量不能大于采购数量", "warning");
                //    return false;
                //}
                $("#frmedit").submit();
            }

            $(".okeydown").on("keyup", function () {
                var value = $(this).val();
                if (isNaN(value) || value == "" || value == "Infinity" || value == "NaN") {
                    $(this).addClass("has-error");
                } else {
                    $(this).removeClass("has-error");
                }
            });

            $("#historyBack").click(function () {
                window.history.go(-1);  //返回上一页
                //window.history.back();  //返回上一页
            });

            $(".okeydown").on("keydown", function () {
                var k = event.keyCode;
                if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                    return true;
                } else {
                    return false;
                }
            });

            //json-content
            function getParam() {
                var postdata = [];
                //主键
                $("#json-content").find('input,select,textarea').each(function (r) {
                    var $this = $(this);
                    var _des = $this.attr('data-des');
                    var _id = $this.attr('id');
                    var type = $this.attr('type');
                    switch (type) {
                        case "checkbox":
                            postdata.push({ des: _des, name: _id, value: $this.is(":checked") });
                            break;
                        case "select":
                            var value = $this.find("option:selected").val() == "" ? null : $this.find("option:selected").val();
                            postdata.push({ des: _des, name: _id, value: value });
                            break;
                        default:
                            var value = $this.val() == "" ? "&nbsp;" : $this.val();
                            postdata.push({ des: _des, name: _id, value: value });
                            break;
                    }

                });
                return JSON.stringify(postdata);
            }

            $(".technologyinfo").click(function () {
                var id = $(this).data('id');
                var options = {
                    id: null,
                    title: '产品信息',
                    width: "100%",
                    height: "100%",
                    url: "../Technology/TechnologyInfo?tId=" + id,
                    shade: 0.3,
                    btn: ['返回'],
                    btnclass: ['btn btn-danger'],
                    callBack: null
                }
                $.modalOpen(options);
            });
            //订单信息弹窗
            function ShowModal(id) {
                var options = {
                    id: null,
                    title: '订单信息',
                    width: "100%",
                    height: "100%",
                    url: "../Order/OrderInfo?OID=" + id,
                    shade: 0.3,
                    btn: ['返回'],
                    btnclass: ['btn btn-danger'],
                    callBack: null
                }
                $.modalOpen(options);
            }


            //弹窗显示供应商
            function ShowCusList() {
                $("#cusList").modal();
            }

            //点击选择供应商
            $(".customerItem").click(function () {
                var id = $(this).attr("data-cusID");
                var name = $(this).attr("data-cusName");
                $("#CustomerName").html(name);
                $("#SupplierIdq").val(id);

                $(".customerItem").addClass("btn-default");
                $(".customerItem").removeClass("btn-primary");
                $(this).addClass("btn-primary");
                $(this).removeClass("btn-default");
            });

            //搜索供应商
            function GetCus() {
                var name = $("#GetCus").val().trim();

                var cusList = $("#CusList .customerItem");
                var num = cusList.size();

                for (var i = 0; i < num; i++) {
                    var cusName = $("#CusList .customerItem").eq(i).attr("data-cusName");
                    if (cusName.indexOf(name) >= 0) {
                        $("#CusList .customerItem").eq(i).show();
                    }
                    else {
                        $("#CusList .customerItem").eq(i).hide();
                    }
                }
            }
        </script>
    }